package com.hckjsec.law.controller;

import java.util.ArrayList;
import java.util.List;

import com.hckjsec.common.core.domain.entity.SysUser;
import com.hckjsec.common.general.MapLongView;
import com.hckjsec.common.general.MapStringView;
import com.hckjsec.common.utils.ComUtil;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.law.domain.LawCaseType;
import com.hckjsec.law.domain.WebLawConfig;
import com.hckjsec.law.domain.WebLawConsulling;
import com.hckjsec.law.service.ILawCaseTypeService;
import com.hckjsec.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.law.domain.WebLawCompany;
import com.hckjsec.law.service.IWebLawCompanyService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公共法律服务平台单位配置Controller
 * 
 * @author cyt
 * @date 2021-11-04
 */
@RestController
@RequestMapping("/system/lawcompany")
public class WebLawCompanyController extends BaseController
{
    @Autowired
    private IWebLawCompanyService webLawCompanyService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ILawCaseTypeService lawCaseTypeService;

    /**
     * 查询公共法律服务平台单位配置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WebLawCompany webLawCompany)
    {
        startPage();
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        if (role < 8 ){
            webLawCompany.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawCompany> list = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        return getDataTable(list);
    }

    /**
     * 导出公共法律服务平台单位配置列表
     */
    @Log(title = "公共法律服务平台单位配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WebLawCompany webLawCompany)
    {
        int role = ComUtil.getHighestRole(SecurityUtils.getLoginUser().getUser().getRoles());
        if (role < 8 ){
            webLawCompany.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawCompany> list = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        ExcelUtil<WebLawCompany> util = new ExcelUtil<WebLawCompany>(WebLawCompany.class);
        return util.exportExcel(list, "company");
    }

    /**
     * 获取公共法律服务平台单位配置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(webLawCompanyService.selectWebLawCompanyById(id));
    }

    /**
     * 新增公共法律服务平台单位配置
     */
    @Log(title = "公共法律服务平台单位配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WebLawCompany webLawCompany)
    {
        webLawCompany.setCreateBy(SecurityUtils.getUsername());
        return toAjax(webLawCompanyService.insertWebLawCompany(webLawCompany));
    }

    /**
     * 修改公共法律服务平台单位配置
     */
    @Log(title = "公共法律服务平台单位配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WebLawCompany webLawCompany)
    {
        webLawCompany.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(webLawCompanyService.updateWebLawCompany(webLawCompany));
    }

    /**
     * 删除公共法律服务平台单位配置
     */
    @Log(title = "公共法律服务平台单位配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(webLawCompanyService.deleteWebLawCompanyByIds(ids));
    }

    /**
     * 获取公共法律服务单位配置list信息
     */
    @GetMapping("/getLawCompanyList")
    public AjaxResult getLawCompanyList()
    {
        List<MapLongView> htList = new ArrayList<>();
        WebLawCompany webLawCompany = new WebLawCompany();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            webLawCompany.setDeptId(SecurityUtils.getDeptId());
        }
        List<WebLawCompany> list = webLawCompanyService.selectWebLawCompanyList(webLawCompany);
        list.forEach(kv->{
            htList.add(new MapLongView(kv.getCompanyName(),kv.getId()));
        });
        return AjaxResult.success(htList);
    }


    /**
     * 获取公共法律服务单位配置list信息
     */
    @GetMapping("/getLawUserList/{id}")
    public AjaxResult getLawUserList(@PathVariable("id") Long id)
    {
        WebLawCompany webLawCompany = webLawCompanyService.selectWebLawCompanyById(id);
        Long  deptId = webLawCompany.getDeptId();
        List<MapLongView> htList = new ArrayList<>();
        List<SysUser> list = userService.initUserList();
        list.forEach(kv->{
            if (deptId.equals(kv.getDeptId())){
                htList.add(new MapLongView(kv.getNickName(),kv.getUserId()));
            }
        });
        return AjaxResult.success(htList);
    }


    /**
     * 获取案件类型业务范围信息
     */
    @GetMapping("/getLawCaseTypeList/{id}")
    public AjaxResult getLawCaseTypeList(@PathVariable("id") Long id)
    {
        WebLawCompany webLawCompany = webLawCompanyService.selectWebLawCompanyById(id);
        String companyType = webLawCompany.getCompanyType();
        LawCaseType lawCaseType = new LawCaseType();
        lawCaseType.setState("true");
        lawCaseType.getParams().put("number", companyType);
        List<LawCaseType> lawCaseTypeList = lawCaseTypeService.selectLawCaseTypeList(lawCaseType);
        List<MapStringView> htList = new ArrayList<>();
        lawCaseTypeList.forEach(kv->{
                htList.add(new MapStringView(kv.getTypeName(),kv.getTypeNumber()));
        });
        return AjaxResult.success(htList);
    }
}
